package com.hm.PushCaseService.control;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hm.PushCaseService.framework.service.base.IBaseService;
import com.hm.PushCaseService.framework.service.token.ITokenService;
import com.hm.PushCaseService.pojo.response.TokenResBean;
import com.hm.PushCaseService.pojo.response.UserBean;
import com.hm.PushCaseService.service.captcha.ICaptchaService;
import com.hm.PushCaseService.service.user.IUserService;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Random;


@RestController
@RequestMapping(value = "/main", produces = {"application/json;charset=UTF-8"})
public class PCS_000_000_002<T> {

    private static final Logger logger = LoggerFactory.getLogger(PCS_000_000_002.class);

    private final IBaseService<TokenResBean> iBaseService;

    private final ICaptchaService iCaptchaService;

    private final IUserService iUserService;

    private final ITokenService iTokenService;

    public PCS_000_000_002(ITokenService iTokenService, IBaseService<TokenResBean> iBaseService, ICaptchaService iCaptchaService, IUserService iUserService) {
        this.iBaseService = iBaseService;
        this.iCaptchaService = iCaptchaService;
        this.iUserService = iUserService;
        this.iTokenService = iTokenService;
    }

    //登录
    @PostMapping(value = "/000_000_002")
    public String doPost(HttpServletRequest request) {
        //基本处理（校验session合法性）
        HashMap<String, String> resMap = iBaseService.handleRequest(request);
        String state = resMap.get("state");
        String msg = resMap.get("msg");
        String body = resMap.get("body");
        String secretKey = resMap.get("secretKey");
        if (!state.equals("000")) {
            return iBaseService.generateFail(msg);
        }
        //验证
        JSONObject jsonObject = (JSONObject) JSON.parse(body);
        String captchaKey = jsonObject.getString("captchaKey");
        String captcha = jsonObject.getString("captcha");
        String userName = jsonObject.getString("userName");
        String userPwd = jsonObject.getString("pwd");

        //非空
        if (captcha == null || captcha.isEmpty()) {
            return iBaseService.generateFail("验证码为空");
        }
        if (userName == null || userName.isEmpty() || userPwd == null || userPwd.isEmpty()) {
            return iBaseService.generateFail("用户名或密码为空");
        }

        //验证码
        HashMap<String, String> captchaVerify = iCaptchaService.verify(captcha, captchaKey);
        if (!captchaVerify.get("state").equals("000")) {
            return iBaseService.generateFail(captchaVerify.get("msg"));
        }

        //用户->查库
        UserBean userBean = iUserService.getUserForName(userName);
        if (userBean == null) {
            return iBaseService.generateFail("用户不存在");
        }
        String pwd = userBean.userPwd;
        if (!pwd.equals(userPwd)) {
            return iBaseService.generateFail("密码错误");
        }
        //完成->保存token到redis
        long userId = userBean.userId;
        String token = iTokenService.saveToken(String.valueOf(userId));
        TokenResBean bean = new TokenResBean();
        bean.token = token;
        bean.userName = userBean.userName;
        return iBaseService.generateSucceed(secretKey, bean);
    }
}
